﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Linq;
using System.ServiceModel.Channels;
using System.Configuration;

namespace PermitsAndVisasLeadsSyncher.Service_Proxy
{
    internal class LeadServiceRequest:IDisposable
    {
        private pandvWSDLPortTypeClient wsdlPortTypeClient;
        
        public LeadServiceRequest()
        {
            wsdlPortTypeClient = GetPortTypeClient();
        }

        public void Dispose()
        {
            wsdlPortTypeClient.Close();
        }

        private pandvWSDLPortTypeClient GetPortTypeClient()
        {
            CustomBinding customBinding = new CustomBinding(new BindingElement[1]
        {
          (BindingElement) new CustomTextMessageBindingElement("ISO-8859-1", "text/xml", MessageVersion.Soap11)
        });
            HttpTransportBindingElement transportBindingElement = new HttpTransportBindingElement() { MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = (long)int.MaxValue };
            customBinding.Elements.Add((BindingElement)transportBindingElement);
            wsdlPortTypeClient = new pandvWSDLPortTypeClient();
            wsdlPortTypeClient.Endpoint.Binding = (Binding)customBinding;
            return wsdlPortTypeClient;
        }
        public XmlNodeList GetLeads(string from, string lastLeadDate)
        {
           // wsdlPortTypeClient = GetPortTypeClient();
            string xml2 = wsdlPortTypeClient.getleads(ConfigurationManager.AppSettings["leadsUsername"], ConfigurationManager.AppSettings["leadsPassword"], from, lastLeadDate);
            XmlDocument xmlDocument2 = new XmlDocument();
            xmlDocument2.LoadXml(xml2);
            XmlNodeList xmlNodeList2 = xmlDocument2.SelectNodes("/XML_Info/data");
            Console.WriteLine("Total number of leads to sync since last sync up with www.permitsandvisas.com are : {0}", (object)xmlNodeList2.Count);
            return xmlNodeList2;
        }

        public XmlNodeList GetLeadsInfo()
        {
            //wsdlPortTypeClient = GetPortTypeClient();
            string xml1 = wsdlPortTypeClient.getleadsinfo(ConfigurationManager.AppSettings["leadsInfoUsername"], ConfigurationManager.AppSettings["leadsInfoPassword"]);
            XmlDocument xmlDocument1 = new XmlDocument();
            xmlDocument1.LoadXml(xml1);
            XmlNodeList xmlNodeList1 = xmlDocument1.SelectNodes("/XML_Info/data");
            return xmlNodeList1;
        }

       
    }
}
